const orderServiceModel = require('../model/orderServiceModel');

//查询所有订单
module.exports.find = async function (searchObj, page) {
    if (page.size) {
        //current当前页，size一页显示多少条数据
        let { current, size } = page;
        // 获取所有服务的总数
        let total = await orderServiceModel.countDocuments(searchObj);
        // 获取总页数 Math.ceil:向上取整
        let totalPage = Math.ceil(total / size);
        // 当前页显示的数据
        let rows = await orderServiceModel
            .find(searchObj)
            .populate({ path: 'serviceId' })
            .populate({ path: 'userId' })
            .populate({ path: 'adminId' })
            .populate({ path: 'shopDoorId' })
            .limit(parseInt(size))//条数
            .skip((current - 1) * size);
        return { current, size, total, totalPage, rows };
    } else {
        return await orderServiceModel
            .find(searchObj)
            .populate({ path: 'serviceId' })
            .populate({ path: 'userId' })
            .populate({ path: 'adminId' })
            .populate({ path: 'shopDoorId' })
    }
}

//增加服务
module.exports.insert = async function (service) {
    return await orderServiceModel.create(service);
}
//删除服务
module.exports.delete = async function (id) {
    return await orderServiceModel.deleteOne({ _id: id });
}
// 通过服务的id查询
module.exports.findById = async function (id) {
    return await orderServiceModel.findById({ _id: id });
}